<?php
	class MainController extends Controller{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Akademik";
		public $subMenu = "";
		public $title = "";
		public $loginUser;
		
		public function init() {
			parent::init();
			if (!LoginUser::isLogin("akademik")){
				//$this->redirect(array('login/akademik','error'=>Yii::t("label", "Username atau password salah")));
				LoginUser::redirectToLoginPage("akademik");
			}else{
				$this->loginUser = LoginUser::getUser();
			}						
		}
		
		public function actionIndex(){			
			LoginUser::getPermission("akademik_pmb");
			$this->subMenu = "Penerimaan Murid Baru";
			$data=array();
			
			$arrDepartment = MDepartment::model()->findAll("is_active = 1");
			$department_id = $arrDepartment == null ?"":$arrDepartment[0]->id;

			$arrTahunAjaran = MTahunAjaran::model()->findAll("is_active = 1 and department_id =:deptId",array(":deptId"=>$department_id));
			
			$tahunAjaranActive = CMTahunAjaran::getTahunAjaranActive($department_id);
			$tahun_ajaran_id = $tahunAjaranActive->id;
			
			//$data=array("department_id"=>"");
			$data["arrDepartment"] =$arrDepartment;
			$data["department_id"] =$department_id;
			$data["arrTahunAjaran"] =$arrTahunAjaran;
			$data["tahun_ajaran_id"] =$tahun_ajaran_id;
						
			$this->layout ="//layouts/ly_blank";						
			$this->render('index',$data);
		}
		
		public function actionPMBSummaryList(){
			$department_id = Yii::app()->request->getParam("department_id","1");
			$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");
			
			$conn = Yii::app()->db;
			$onlyActive = Yii::app()->request->getParam("activeOnly",false);			
			try{
				$strFields="a.id,a.department_id,trial_id,sid,b.name as tahun_ajaran,a.name,if(gender='P','Perempuan','Laki-laki') as gender,birth_date,address,home_phone,handphone,email,a.is_active";
				$params = array("dept"=>$department_id);
				if ($onlyActive){
					$sql ="select $strFields from m_student a inner join m_tahun_ajaran b on a.tahun_ajaran_id = b.id where a.department_id = :dept and a.is_active = true and status_siswa = 'TRIAL'";
				}else{
					$sql ="select $strFields from m_student a inner join m_tahun_ajaran b on a.tahun_ajaran_id = b.id where a.department_id = :dept and status_siswa = 'TRIAL'";
				}
				
				if ($tahun_ajaran_id != ""){
					$sql .= " and tahun_ajaran_id = :tahunAjaranId";
					$params[":tahunAjaranId"] = $tahun_ajaran_id;					
				}
				$sql .= " order by id desc";				
				$command = $conn->createCommand($sql);				
				$rs =$command->query($params);
				
				$rw = $rs->readAll();
				$this->result["actionStatus"] =0;
				$this->result["actionMessage"] ="";
				$this->result["actionError"] =array();
				$this->result["model"] = $rw;
			}catch(Exception $e){
				$this->result["actionStatus"] =2;
				$this->result["actionMessage"] =$e->getMessage();
			}			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}
		public function actionMigrasiStudent(){		
			$conn = Yii::app()->db;
			$conn2 = Yii::app()->db2;
			$sql="select * from t_student";
			$cmd = $conn2->createCommand($sql);
			
			
			$rs = $cmd->query();
			foreach($rs as $rw){
				/*
				Array ( [sid] => 0001.13 [type] => Registered [first_name] => Malika Nahla Athaya 
				[last_name] => Noor [nick_name] => Alika [birth_date] => 2010-12-05 [birth_place] => 
				[gender] => P [citizenship] => Indonesia 
				[religion] => islam [address] => Jl. Raya Condet Rt. 009/05 No. 59 Cililitan. 
				[propinsi] => DKI Jakarta [kota] => Jakarta Timur [area] => Kramat Jati [post_code] => 13640 
				[home_phone] => 021-8000046 [register_datetime] => 2013-02-01 [is_active] => 1 [system_datetime] => 2013-03-26 15:36:39 [user_id] => 1 
				[parent_password] => [level] => Playgroup [exclude_va] => 0 ) 
				*/
				print_r($rw);
				
				$sql = "insert into m_student (department_id,sid,name,nick_name,birth_place,birth_date,gender,citizenship,religion,address,propinsi,kota,area,post_code,home_phone,register_date,is_active,create_bca_va) values (
				1,:sid,:name,:nick,:bPlace,:bDate,:gender,:citizen,:religion,:addr,:prop,:kota,:area,:postcode,:homephone,:regdate,:isactive,:createva)";			
				$cmd=$conn->createCommand($sql);
				$result = $cmd->execute(array(
						":sid"=>$rw["sid"],
						":name"=>$rw["first_name"] ." " . $rw["last_name"],
						":nick"=>$rw["nick_name"],
						":bPlace"=>$rw["birth_place"],
						":bDate"=>$rw["birth_date"],
						":gender"=>$rw["gender"],
						":citizen"=>$rw["citizenship"],
						":religion"=>$rw["religion"],
						":addr"=>$rw["address"],
						":prop"=>$rw["propinsi"],
						":kota"=>$rw["kota"],
						":area"=>$rw["area"],
						":postcode"=>$rw["post_code"],
						":homephone"=>$rw["home_phone"],
						":regdate"=>$rw["register_datetime"],
						":isactive"=>$rw["is_active"],
						":createva"=>$rw["exclude_va"] == "0" ?1:0
					)
				);			
				echo "</br></br>$result </br></br></br></br></br>";
			}
			
			
			
			$conn2->active=false; 
		
		}
		public function actionForm(){
			$op = Yii::app()->request->getParam("op","add");
			$department_id = Yii::app()->request->getParam("department_id","");
			$propinsi =Yii::app()->request->getParam("propinsi","");
			$kota =Yii::app()->request->getParam("kota","");
			$area =Yii::app()->request->getParam("area","");
			
			$data = MStudent::model()->getAttributes();
			
			$data["arrReligion"] = array("","Islam","Khatolik","Protestan","Hindu","Budha");
			$data["arrEtnic"] = array("","Bali","Batak","Jawa","Padang","Sunda","Lainnya");
			
			
			if ($op == "add"){
				LoginUser::getPermission("akademik_pmb_add");
				$this->subMenu = Yii::t("label","Tambah Data Siswa");
				//default data for add
				$data = array_merge($data,MStudent::model()->getMetaData()->attributeDefaults);
				$data["op"] ="add";
				$data["department_id"] =$department_id;
				$actTahunAjaran = CMTahunAjaran::getTahunAjaranActive($department_id);
				$data["tahun_ajaran"] = $actTahunAjaran;
				try{
					$data["trial_id"] = CMStudent::getNewTID($department_id);
				}catch (Exception $e){
					$data["trial_id"] = null;
				}	
								
				$data["imgPhotoProfile"] = "images/blank-photo.jpg";
				$data["birth_date"] = "01-Jan-2000";
				$data["tingkatan_name"] = "";
				$data["kelas_name"] = "";
				
				$arrPropinsi = array("");
				$listPropinsi = SysArea::model()->findAll(array('select'=>'propinsi', 'distinct'=>true,));
				foreach ($listPropinsi as $row){
					array_push($arrPropinsi,$row->propinsi);
				}
				$data["arrPropinsi"] = $arrPropinsi;
				if ($propinsi == ""){
					$propinsi = $arrPropinsi != null ? "" : $arrPropinsi[0];
				}
					
				$arrKota = array("");
				$listKota = SysArea::model()->findAll(array('select'=>'kabupaten', 'distinct'=>true,'condition'=>"propinsi = '$propinsi'"));
				foreach ($listKota as $row){
					array_push($arrKota,$row->kabupaten);
				}
				$data["arrKota"] = $arrKota;
				if($kota == ""){
					$kota  = $arrKota != null ? "" : $arrKota[0];
				}
					
				$arrArea = array("");
				$listArea = SysArea::model()->findAll(array('select'=>'kecamatan', 'distinct'=>true,'condition'=>"propinsi = '$propinsi' and kabupaten ='$kota' "));
				foreach ($listArea as $row){
					array_push($arrArea,$row->kecamatan);
				}
				$data["arrArea"] = $arrArea;
				if($area == ""){
					$area  = $arrArea != null ? "" : $arrArea[0];
				}
				
				$data["propinsi"]=$propinsi;
				$data["kota"]=$kota;
				$data["area"]=$area;
				
					
				$this->layout ="//layouts/ly_blank";
				$this->render('form',$data);								
			}else if($op == "edit"){
				LoginUser::getPermission("akademik_pmb_edit");
				$this->subMenu = Yii::t("label","Ubah Data Siswa");
				$id = Yii::app()->request->getParam("id","0");
				$data["id"] = $id;
								
				$student = MStudent::model()->findByPk(array("id"=>$id));
				if ($student != null){
					$dataKeys = array_keys($student->attributes);
					for ($i=0;$i < sizeof($dataKeys) ;$i++){
						$key =$dataKeys[$i];
						$data[$key] = $student[$key];
					}
					$data["op"] = "edit";
					$data["tahun_ajaran"] = MTahunAjaran::model()->findByPk($student["tahun_ajaran_id"]);
					$birthDate = date_format(date_create_from_format( "Y-m-d", $data["birth_date"]),"d-M-Y");
					$data["birth_date"] = $birthDate;
					$imgPhotoProfile = "images/photo_profile/" . $data["department_id"]. "_" . $data["sid"] .".jpg";
					if (file_exists($imgPhotoProfile)){
						$data["imgPhotoProfile"] = $imgPhotoProfile;
					}else{
						$data["imgPhotoProfile"] = "images/blank-photo.jpg";
					}								
					$rw = CMStudent::getActiveClass("",$student);
					$data["tingkatan_name"] = $rw["tingkatan_name"];
					$data["kelas_name"] = $rw["kelas_name"];
					
					$arrPropinsi = array("");
					$listPropinsi = SysArea::model()->findAll(array('select'=>'propinsi', 'distinct'=>true,));
					foreach ($listPropinsi as $row){
						array_push($arrPropinsi,$row->propinsi);
					}
					$data["arrPropinsi"] = $arrPropinsi;
					$propinsi = $student["propinsi"];
						
					$arrKota = array("");
					$listKota = SysArea::model()->findAll(array('select'=>'kabupaten', 'distinct'=>true,'condition'=>"propinsi = '$propinsi'"));
					foreach ($listKota as $row){
						array_push($arrKota,$row->kabupaten);
					}
					$data["arrKota"] = $arrKota;
					$kota  = $student["kota"];
						
					$arrArea = array("");
					$listArea = SysArea::model()->findAll(array('select'=>'kecamatan', 'distinct'=>true,'condition'=>"propinsi = '$propinsi' and kabupaten ='$kota' "));
					foreach ($listArea as $row){
						array_push($arrArea,$row->kecamatan);
					}
					$data["arrArea"] = $arrArea;
					$area  = $student["area"];
					
					$data["propinsi"]=$student["propinsi"];
					$data["kota"]=$student["kota"];
					$data["area"]=$student["area"];
					
				}else{
					echo "Record not found";
					return;	
				}
				$this->layout ="//layouts/ly_blank";
				$this->render('form',$data);
			}
		}

		public function actionGetFamily(){
			$id = Yii::app()->request->getParam("student_id","");
			$family = CMStudent::getFamilyAjx($id);
			$this->result["actionStatus"] =0;
			$this->result["actionMessage"] ="";
			$this->result["actionError"] =array();
			$this->result["model"] = $family;
			$this->returnJSON($this->result);
				
		} 
		 
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
		public function actionedit(){
			//header('Location: ' . $_SERVER['HTTP_REFERER']);
			$familyDataJSON = Yii::app()->request->getParam("familyData","");
			$arrFamily =json_decode($familyDataJSON);
			
			
			$photo = Yii::app()->request->getParam("webcamPhoto","");
			$op = Yii::app()->request->getParam("op","add");
			$status = Yii::app()->request->getParam("is_active",0) === "on"?1:0 ;		
			if ($op=="add"){
				LoginUser::getPermission("akademik_pmb_add");
				try{
					$student = new MStudent;
					$student->department_id = Yii::app()->request->getParam("department_id",null);
					$tahunAjaranAktif = CMTahunAjaran::getTahunAjaranActive($student->department_id);
					$student->tahun_ajaran_id = $tahunAjaranAktif["id"];					
					//$registerDate = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("register","01-Jan-2000")),"Y-m-d");
					$student->register_date = date("Y-m-d");
					$student->trial_id =CMStudent::getNewTID($student->department_id);
					
					//$student->sid=CMStudent::getNewSID($student->department_id);
					$student->name=Yii::app()->request->getParam("name",null);
					$student->nick_name=Yii::app()->request->getParam("nick_name",null);
					$student->birth_place=Yii::app()->request->getParam("birth_place",null);
					$birthDate = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("birth_date","01-Jan-2000")),"Y-m-d");
					$student->birth_date=$birthDate;
					$student->gender=Yii::app()->request->getParam("gender","L");
					$student->citizenship=Yii::app()->request->getParam("citizenship","WNI");
					$student->religion=Yii::app()->request->getParam("religion",null);
					$student->ethnic=Yii::app()->request->getParam("ethnic",null);
					$student->address=Yii::app()->request->getParam("address",null);
					$student->propinsi=Yii::app()->request->getParam("propinsi",null);
					$student->kota=Yii::app()->request->getParam("kota",null);
					$student->area=Yii::app()->request->getParam("area",null);
					$student->post_code=Yii::app()->request->getParam("post_code",null);
					$student->home_phone=Yii::app()->request->getParam("home_phone",null);
					$student->handphone=Yii::app()->request->getParam("handphone",null);
					$student->email=Yii::app()->request->getParam("email",null);
					$student->asal_sekolah=Yii::app()->request->getParam("asal_sekolah",null);
					$student->keterangan_asal_sekolah=Yii::app()->request->getParam("keterangan_asal_sekolah",null);
					$student->gol_darah=Yii::app()->request->getParam("gol_darah",null);
					$student->berat_kg=Yii::app()->request->getParam("berat_kg",0);
					$student->tinggi_cm=Yii::app()->request->getParam("tinggi_cm",0);
					$student->riwayat_penyakit=Yii::app()->request->getParam("riwayat_penyakit",null);				
					$student->alamat_surat =Yii::app()->request->getParam("alamat_surat","");
					$student->restriction =Yii::app()->request->getParam("restriction","Ayah");
					$student->payment_method =Yii::app()->request->getParam("payment_method","Cash");
					$student->ref_payment =Yii::app()->request->getParam("ref_payment","");
					$student->status_siswa="TRIAL";
					$student->is_active =$status;
					$student->insert_date = date("Y-m-d H:i:s");
					$student->last_user_id = $this->loginUser["id"];					
					$photoProfile = $_FILES["photoProfile"];					
					if (substr($photoProfile["type"],0,6) == "image/"){
						rename($photoProfile["tmp_name"], "images/photo_profile/tmp_".$student->department_id. "_" . $student->sid . ".jpg");
					}
					if ($photo != ""){
						file_put_contents("images/photo_profile/tmp_".$student->department_id. "_" . $student->sid . ".jpg", base64_decode($photo));
					}

					$transaction = Yii::app()->db->beginTransaction();
					
					if ($student->save(true)){
						if (file_exists("images/photo_profile/tmp_".$student->department_id. "_" . $student->sid . ".jpg")){
							rename("images/photo_profile/tmp_".$student->department_id. "_" . $student->sid . ".jpg", "images/photo_profile/".$student->department_id. "_" . $student->sid . ".jpg");
						}
						// add Family Data
						foreach ($arrFamily as $family){
							if ($family->op == "delete"){
								$studentFamily = MStudentFamily::model()->findByPk($family->id);
								$studentFamily->delete();
							}else{
								if ($family->op == "edit"){
									$studentFamily = MStudentFamily::model()->findByPk($family->id);
								}
								if ($family->op == "add"){
									$studentFamily = new MStudentFamily();
								}
								$studentFamily->student_id = $student->id;
								$studentFamily->family_type = $family->family_type;
								$studentFamily->is_alm = $family->is_alm == "No"?0:1;
								$studentFamily->name = $family->name;
								$studentFamily->home_phone = $family->home_phone;
								$studentFamily->office_phone = $family->office_phone;
								$studentFamily->mobile_phone = $family->mobile_phone;
								$studentFamily->email_address = $family->email_address;
								$studentFamily->address = $family->address;
								$studentFamily->priority_contact = $family->priority_contact == "No" ? 0 :1;
								if ($studentFamily->save(true)){
									
								}else{
									print_r($studentFamily->getErrors());
									$transaction->rollback();
									exit();
								}
							}
						}
						$transaction->commit();
						echo "<script>
							alert('" .Yii::t("message","Data Sudah di tambahkan...!")."');
							window.close();					
						</script>";
						exit;
					}else{
						if (file_exists("images/photo_profile/tmp_".$student->department_id. "_" . $student->sid . ".jpg")){
							unlink("images/photo_profile/tmp_".$student->department_id. "_" . $student->sid . ".jpg");
						}
						print_r($student->getErrors());
						$transaction->rollback();
						exit();
					}
				}catch (Exception $e){ 
					echo $e->getMessage();
				}
			}else if($op=="edit"){
				
				LoginUser::getPermission("akademik_pmb_edit");
				try{
					$id = Yii::app()->request->getParam("id",null);
					$student = MStudent::model()->findByPk($id);
					if ($student == null){
						echo "Student Record Not found...!";
						return;
					}
					$student->department_id = Yii::app()->request->getParam("department_id",null);
					$student->tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id",null);					
					//$registerDate = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("register","01-Jan-2000")),"Y-m-d");
					//$student->register_date = $registerDate;
					//$student->sid=Yii::app()->request->getParam("sid",null); //nilai tidak bisa di rubah
					$student->name=Yii::app()->request->getParam("name",null);
					$student->nick_name=Yii::app()->request->getParam("nick_name",null);
					$student->birth_place=Yii::app()->request->getParam("birth_place",null);
					$birthDate = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("birth_date","01-Jan-2000")),"Y-m-d");
					$student->birth_date=$birthDate;
					$student->gender=Yii::app()->request->getParam("gender","L");
					$student->citizenship=Yii::app()->request->getParam("citizenship","WNI");
					$student->religion=Yii::app()->request->getParam("religion",null);
					$student->ethnic=Yii::app()->request->getParam("ethnic",null);
					$student->address=Yii::app()->request->getParam("address",null);
					$student->propinsi=Yii::app()->request->getParam("propinsi",null);
					$student->kota=Yii::app()->request->getParam("kota",null);
					$student->area=Yii::app()->request->getParam("area",null);
					$student->post_code=Yii::app()->request->getParam("post_code",null);
					$student->home_phone=Yii::app()->request->getParam("home_phone",null);
					$student->handphone=Yii::app()->request->getParam("handphone",null);
					$student->email=Yii::app()->request->getParam("email",null);
					$student->asal_sekolah=Yii::app()->request->getParam("asal_sekolah",null);
					$student->keterangan_asal_sekolah=Yii::app()->request->getParam("keterangan_asal_sekolah",null);
					$student->gol_darah=Yii::app()->request->getParam("gol_darah",null);
					$student->berat_kg=Yii::app()->request->getParam("berat_kg",0);
					$student->tinggi_cm=Yii::app()->request->getParam("tinggi_cm",0);
					$student->riwayat_penyakit=Yii::app()->request->getParam("riwayat_penyakit",null);
					$student->alamat_surat =Yii::app()->request->getParam("alamat_surat","");
					$student->restriction =Yii::app()->request->getParam("restriction","Ayah");
					$student->payment_method =Yii::app()->request->getParam("payment_method","Cash");
					$student->ref_payment =Yii::app()->request->getParam("ref_payment","");
					$student->status_siswa="TRIAL";
					$student->is_active =$status;
					$student->last_update_date = date("Y-m-d H:i:s");
					$student->last_user_id = $this->loginUser["id"];
						
					
					$photoProfile = $_FILES["photoProfile"];
					//$photoProfile["type"] == "image/jpeg"
					if (substr($photoProfile["type"],0,6) == "image/"){
						rename($photoProfile["tmp_name"], "images/photo_profile/tmp_".$student->department_id. "_" . $student->sid . ".jpg");
					}
					if ($photo != ""){
						file_put_contents("images/photo_profile/tmp_".$student->department_id. "_" . $student->sid . ".jpg", base64_decode($photo));
					}
					
					$transaction = Yii::app()->db->beginTransaction();
					if ($student->save(true)){						
						// add Family Data
						foreach ($arrFamily as $family){
							//print_r($family);
							if ($family->op == "delete"){
								$studentFamily = MStudentFamily::model()->findByPk($family->id);
								$studentFamily->delete();								
							}else{
								if ($family->op == "edit"){
									$studentFamily = MStudentFamily::model()->findByPk($family->id);
								}
								if ($family->op == "add"){
									$studentFamily = new MStudentFamily();
								}
								$studentFamily->student_id = $student->id;
								$studentFamily->family_type = $family->family_type;
								$studentFamily->is_alm = $family->is_alm == "No"?0:1;
								$studentFamily->name = $family->name;
								$studentFamily->home_phone = $family->home_phone;
								$studentFamily->office_phone = $family->office_phone;
								$studentFamily->mobile_phone = $family->mobile_phone;
								$studentFamily->email_address = $family->email_address;
								$studentFamily->address = $family->address;
								$studentFamily->priority_contact = $family->priority_contact == "Yes" ?1:0;
								if ($studentFamily->save(true)){						
								}else{
									print_r($studentFamily->getErrors());
									$transaction->rollback();
									exit();
								}
							}
						}
						
						if (file_exists("images/photo_profile/tmp_".$student->department_id. "_" . $student->sid . ".jpg")){
							rename("images/photo_profile/tmp_".$student->department_id. "_" . $student->sid . ".jpg", "images/photo_profile/".$student->department_id. "_" . $student->sid . ".jpg");
						}
						
						$transaction->commit();
						echo "<script>
							alert('" .Yii::t("message","Data telah di perbaharui...!")."');
							window.close();
						</script>";
						exit;
					}else{
						if (file_exists("images/photo_profile/tmp_".$student->department_id. "_" . $student->sid . ".jpg")){
							unlink("images/photo_profile/tmp_".$student->department_id. "_" . $student->sid . ".jpg");
						}
						print_r($student->getErrors());
						$transaction->rollback();
						exit();
					}
				}catch (Exception $e){
					echo $e->getMessage();
				}
			}else if($op=="del"){
				if (!LoginUser::getPermission("akademik_pmb_del",null,false)){
					$this->result["actionStatus"] = 1;
					$this->result["actionMessage"] = Yii::t("dialog", "Anda tidak memiliki ijin untuk menghapus data ini !");					
					header('Content-type:application/json');
					echo CJSON::encode($this->result);
					return;					
				}
				
				try{
					$id = Yii::app()->request->getParam("id",null);
					$student = MStudent::model()->findByPk($id);
					
					if ($student == null){
						$this->result["actionStatus"] = 1;
						$this->result["actionMessage"] = Yii::t("dialog", "Data tidak ditemukan.");						
					}else{
						$student["is_active"] = 0;
						$student->delete_date = date("Y-m-d H:i:s");
						$student->last_user_id = $this->loginUser["id"];
						
						$transaction = Yii::app()->db->beginTransaction();
						
						MStudentFamily::model()->deleteAll("student_id = :studentId",array(":studentId"=>$student->id));
						
						if ($student->delete()){
							$this->result["actionStatus"] = 0;
							$transaction->commit();
						}else{
							$this->result["actionStatus"] = 1;
							$this->result["actionMessage"] = Yii::t("dialog", "Data tidak dapat dihapus.");
							$this->result["actionError"] = $student->getErrors();
							$transaction->rollback();
						}						
					}
				}catch (Exception $e){
					$transaction->rollback();
					$this->result["actionStatus"] = 2;
					$this->result["actionMessage"] = Yii::t("dialog","Data tidak dapat dihapus");
					$this->result["actionError"] = $e->getMessage();
					
				}
				header('Content-type:application/json');
				echo CJSON::encode($this->result);
			}else if ($op =="activate"){
				if (!LoginUser::getPermission("akademik_pmb_activate",null,false)){
					$this->result["actionStatus"] = 1;
					$this->result["actionMessage"] = Yii::t("dialog", "Anda tidak memiliki ijin untuk merubah status siswa!");
					header('Content-type:application/json');
					echo CJSON::encode($this->result);
					return;
				}
				
				try{
					$id = Yii::app()->request->getParam("id",null);
					$student = MStudent::model()->findByPk($id);						
					if ($student == null){
						$this->result["actionStatus"] = 1;
						$this->result["actionMessage"] = Yii::t("dialog", "Data tidak ditemukan.");
					}else{
						if ($student->sid == "" || $student->sid == null){
							$student->sid = CMStudent::getNewSID($student->department_id);
						}
						
						$student->status_siswa = "ACTIVE";
						if ($student->save()){
							$this->result["actionStatus"] = 0;
							$this->result["actionMessage"] = $student->sid .":" . $student->name ." - " . "telah di aktifkan";
						}else{
							$this->result["actionStatus"] = 1;
							$this->result["actionMessage"] = Yii::t("dialog", "Gagal rubah status siswa!");
							$this->result["actionError"] = $student->getErrors();
						}
					}
				}catch (Exception $e){
					$this->result["actionStatus"] = 2;
					$this->result["actionMessage"] = Yii::t("dialog","Gagal rubah status siswa!");
					$this->result["actionError"] = $e->getMessage();
						
				}
				header('Content-type:application/json');
				echo CJSON::encode($this->result);
			}
		}
		
		public function actiongetWebcam(){
			$data =array("callback"=>Yii::app()->request->getParam("callback",""));
			$this->layout ="//layouts/ly_blank";
			$this->render('webcam',$data);
		}

	}
?>